package com.richdown.movie.util;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang.StringUtils;

public class HttpClientUtil {
	/**
	 * post请求
	 * @param url
	 * @param params
	 * @param charset
	 * @return
	 * @throws Exception
	 */
	public static String post(String url,NameValuePair [] params,String charset) throws Exception{

        HttpClient httpClient = new HttpClient();
        HttpMethod postMethod = new PostMethod(url);
        if (StringUtils.isBlank(charset)) charset = "UTF-8";
        postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
        if (params != null) postMethod.setQueryString(params);
        try
        {
            int statusCode = httpClient.executeMethod(postMethod);
            if (statusCode == HttpStatus.SC_OK)
            {
                return postMethod.getResponseBodyAsString();
            } else
            {
                throw new Exception("HttpClient response status is: " + statusCode);
            }
        } finally
        {
        	postMethod.releaseConnection();
        }
	}
}
